import { graphql } from "../graphql";
import type { Me } from "../me/me.types";
import type { LoginFn, LoginInput, LoginSendVcodeFn, LoginSendVcodeInput } from "./login.types";

export const loginSendVcode: LoginSendVcodeFn = async (input: LoginSendVcodeInput) => {
  const query = `
    mutation LoginSendVcode($input: LoginSendVcodeInput!){
      loginSendVcode(input: $input)
    }
  `;
  const variables = { input };
  const data = await graphql(query, variables);
  return data.loginSendVcode as string;
};

export const login: LoginFn = async (input: LoginInput) => {
  const query = `
    mutation Login($input: LoginInput!){
      login(input: $input) {
        id nickname createdAt token
      }
    }
  `;
  const variables = { input };
  const data = await graphql(query, variables);
  return data.login as Me;
};
